﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HypermarketSerialPort._1_Model
{
    class CancelTransactionManager
    {
        private static CancelTransactionManager mInstance;
        private DBManager mDBManager;

        #region CancelTransactionManager
        //only create one instance of CancelTransactionManager
        public static CancelTransactionManager getInstance()
        {
            if (mInstance == null)
            {
                mInstance = new CancelTransactionManager();
            }
            return mInstance;
        }
        #endregion


        #region Constructor
        private CancelTransactionManager()
        {
            mDBManager = DBManager.getInstance();
        }
        #endregion

        #region Operation for cancel the transactions
        public string OperationCancelTransaction(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashierDict)
        {
            if (receivedInfoList.Count() == Constant.TWO)
            {
                CashierRegister cashierRegister;
                string productID = receivedInfoList[Constant.ONE];
                string machineID = receivedInfoList[Constant.ZERO];
                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return CancelTransaction(cashierRegister, productID);
                }
                else
                {
                    return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                }
            }
            else if (receivedInfoList.Count() == Constant.ONE)
            {
                CashierRegister cashierRegister;
                string machineID = receivedInfoList[Constant.ZERO];
                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return CancelAllTransaction(cashierRegister);
                }
                else
                {
                    return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                }

            }
            else
            {
                return Constant.PROGRAM_ERROR + Constant.NEWLINE;
            }
        }

        private static string CancelTransaction(CashierRegister cashierRegister, string productID)
        {
            if (cashierRegister.isTransactionStarted())
            {
                if (cashierRegister.CancelTransaction(productID))
                    return Constant.CANCEL_TRANSACTION_MSG + productID;
                else
                    return Constant.NO_ITEM + Constant.NEWLINE;
            }
            else
            {
                return Constant.START_TRANSACTION_FIRST + Constant.NEWLINE;
            }
        }

        private string CancelAllTransaction(CashierRegister cashierRegister)
        {
            if (cashierRegister.isTransactionStarted())
            {
                cashierRegister.CancelAllTransactions();
                return Constant.CANCEL_ALL_TRANSACTIONS_MSG + Constant.NEWLINE;
            }
            else
            {
                return Constant.START_TRANSACTION_FIRST + Constant.NEWLINE;
            }
        }
        #endregion

    }
}
